﻿using System;
using System.Data;
using System.Drawing;
using System.Text;
using System.Web.UI.WebControls;
using Me.Amon.Da.Db;
using Me.Amon.Da.Db.OMS;
using Me.Amon.Util;
using Me.Amon.WMS.C;

namespace Me.Amon.OMS
{
    public partial class Spu : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                return;
            }

            var user = WebUtil.CurrentUser(Session);
            if (user.Rank < 1)
            {
                return;
            }

            var cat = Request["cat"];
            if (!CharUtil.IsValidateHash(cat))
            {
                cat = "0";
            }

            var dba = new RDBEngine();
            ListCat(dba, cat);

            dba.Reset();
            ListSpu(dba, cat, 0);
        }

        #region 导航
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BnAppendCat_Click(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            Response.Redirect("~/OMS/UpdateCat.aspx?cat=" + node.Value);
        }

        protected void BnAppendSpu_Click(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            Response.Redirect("~/OMS/UpdateSpu.aspx?cat=" + node.Value);
        }
        #endregion

        protected void TvCats_SelectedNodeChanged(object sender, EventArgs e)
        {
            var node = TvCats.SelectedNode;
            if (node == null)
            {
                return;
            }

            var dba = new RDBEngine();
            ListSpu(dba, node.Value, 0);
        }

        #region 目录
        private void ListCat(RDBEngine dba, string cat)
        {
            var root = new TreeNode();
            root.Value = "0";
            root.Selected = (cat == root.Value);
            root.Text = "商品类别";
            TvCats.Nodes.Add(root);

            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, "0");
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);

                LoadCat(dba, node, cat);
            }
        }

        private void LoadCat(RDBEngine dba, TreeNode root, string cat)
        {
            dba.Reset();
            dba.AddTable(DCat.TABLE);
            dba.AddColumn(DCat.ID);
            dba.AddColumn(DCat.NAME);
            dba.AddWhere(DCat.PID, root.Value);
            dba.AddWhere(DCat.STATUS, CCat.STATUE_ENABLED.ToString(), false, false);

            foreach (DataRow row in dba.ExecuteSelect().Rows)
            {
                var node = new TreeNode();
                node.Value = "" + row[DCat.ID];
                node.Selected = (cat == node.Value);
                node.Text = "" + row[DCat.NAME];
                root.ChildNodes.Add(node);

                LoadCat(dba, node, cat);
            }
        }
        #endregion

        private void ListSpu(RDBEngine dba, string cat, int page)
        {
            var sql = new StringBuilder();
            sql.Append("SELECT");
            sql.Append(" a.").Append(DSpu.ID);
            sql.Append(", a.").Append(DSpu.CODE);
            sql.Append(", a.").Append(DSpu.NAME);
            sql.Append(", a.").Append(DSpu.MODE);
            sql.Append(", a.").Append(DSpu.SPEC);
            sql.Append(", a.").Append(DSpu.CQTY);
            sql.Append(", a.").Append(DSpu.CAT_ID).Append(" AS cat");
            sql.Append(", a.").Append(DSpu.MAX_INV);
            sql.Append(", a.").Append(DSpu.MIN_INV);
            sql.Append(", a.").Append(DSpu.VALID_YEAR);
            sql.Append(", a.").Append(DSpu.VALID_MONTH);
            sql.Append(", a.").Append(DSpu.VALID_DAY);
            sql.Append(", a.").Append(DSpu.HAS_POINT);
            sql.Append(", a.").Append(DSpu.IS_VIP);
            sql.Append(", a.").Append(DSpu.STATUS);
            sql.Append(", b.").Append(DSpuBrand.NAME).Append(" AS brand");
            sql.Append(" FROM ").Append(DSpu.TABLE).Append(" a LEFT JOIN ").Append(DSpuBrand.TABLE).Append(" b ON a.").Append(DSpu.BRAND_ID).Append(" = b.").Append(DSpuBrand.ID);
            sql.Append(" WHERE a.").Append(DSpu.CAT_ID).Append(" ='").Append(CharUtil.Text2DB(cat)).Append("'");

            var table = dba.ExecuteSelect(sql.ToString());

            // 每页行数
            int RPP = 2;
            int cnt = table.Rows.Count;
            int num = cnt / RPP;
            int p = page - 1;
            if (p + 2 > num)
            {
                p = num - 2;
            }
            if (p < 0)
            {
                p = 0;
            }

            HlHome.Enabled = num > 0 && page > 0;
            LlPrev.Visible = num > 3 && page > 0;

            HlPage1.CommandArgument = p.ToString();
            HlPage1.ForeColor = p == page ? Color.Red : Color.Blue;
            p += 1;
            HlPage1.Text = p.ToString();

            if (num > 0)
            {
                HlPage2.Visible = true;
                HlPage2.CommandArgument = p.ToString();
                HlPage2.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage2.Text = p.ToString();
            }
            else
            {
                HlPage2.Visible = false;
            }

            if (num > 1)
            {
                HlPage3.Visible = true;
                HlPage3.CommandArgument = p.ToString();
                HlPage3.ForeColor = p == page ? Color.Red : Color.Blue;
                p += 1;
                HlPage3.Text = p.ToString();
            }
            else
            {
                HlPage3.Visible = false;
            }

            LlNext.Visible = num > 3 && page < num - 3;
            HlLast.Enabled = page < num;
            HlLast.CommandArgument = num.ToString();
            LlPage.Text = string.Format("第 {0}/{1} 页", page + 1, num + 1);

            PagedDataSource pds = new PagedDataSource();
            pds.DataSource = table.DefaultView;
            pds.AllowPaging = true;//允许分页
            pds.PageSize = RPP;//单页显示项数
            pds.CurrentPageIndex = page;

            RpSpus.DataSource = pds;
            RpSpus.DataBind();
        }

        #region 分页
        protected void HlHome_Click(object sender, EventArgs e)
        {
            var dba = new RDBEngine();
            var cat = TvCats.SelectedNode.Value;
            ListSpu(dba, cat, 0);
        }

        protected void HlPage1_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage1.CommandArgument);
            var dba = new RDBEngine();
            var cat = TvCats.SelectedNode.Value;
            ListSpu(dba, cat, max);
        }

        protected void HlPage2_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage2.CommandArgument);
            var dba = new RDBEngine();
            var cat = TvCats.SelectedNode.Value;
            ListSpu(dba, cat, max);
        }

        protected void HlPage3_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlPage3.CommandArgument);
            var dba = new RDBEngine();
            var cat = TvCats.SelectedNode.Value;
            ListSpu(dba, cat, max);
        }

        protected void HlLast_Click(object sender, EventArgs e)
        {
            int max = int.Parse(HlLast.CommandArgument);
            var dba = new RDBEngine();
            var cat = TvCats.SelectedNode.Value;
            ListSpu(dba, cat, max);
        }
        #endregion
    }
}